package com.qf.business.user.controller;

import com.qf.business.user.service.ScoreDescService;
import com.qf.business.user.service.StudentService;
import com.qf.commons.base.result.R;
import com.qf.commons.base.result.Rs;
import com.qf.commons.web.aspect.annotation.GetUser;
import com.qf.commons.web.aspect.utils.UserUtils;
import com.qf.data.user.entity.ScoreDesc;
import com.qf.data.user.entity.Student;
import com.qf.data.user.vo.input.StuLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * author Ken
 * create_time 2022/12/2
 */
@RestController
@RequestMapping("/user/stu")
@Slf4j
public class StuController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private ScoreDescService scoreDescService;

    /**
     * 根据openid 查询学生信息
     * @return
     */
    @RequestMapping("/queryByOpenId")
    public R<Student> queryStu(@RequestBody StuLoginVo loginVo){
        Student student = studentService.queryStu(loginVo);
        return Rs.create(student);
    }

    /**
     * 查询用户积分
     * @return
     */
    @GetUser
    @RequestMapping("/querySocre")
    public R<Integer> querySocre(){
        Student student = UserUtils.getUser(Student.class);
        log.debug("[query user score] 查询用户积分 - {}", student);
        Student stu = studentService.getById(student.getId());
        return Rs.create(stu.getScore());
    }

    /**
     * 修改用户积分
     * @param score 修改的积分数
     * @param action 0 - 增加 1 - 扣减
     * @return
     */
    @RequestMapping("/updateScore")
    public R updateUserScore(Integer score, Integer action){
        int result = studentService.updateUserScore(score, action);
        return Rs.create(result);
    }

    @RequestMapping("/updateScoreByUid")
    public R updateUserScore(Integer score, Long uid, Integer action){
        int result = studentService.updateUserScore(score, uid, action);
        return Rs.create(result);
    }

    /**
     * 保存积分流水
     * @return
     */
    @RequestMapping("/insertScoreDesc")
    public R insertScoreDesc(@RequestBody ScoreDesc scoreDesc){
        scoreDescService.save(scoreDesc);
        return Rs.create();
    }
}
